package pl.pages;

import java.util.Calendar;
import java.util.Date;

import org.apache.tapestry.BaseComponent;
import org.apache.tapestry.annotations.Parameter;
import org.apache.tapestry.event.PageBeginRenderListener;
import org.apache.tapestry.event.PageEvent;
import org.apache.tapestry.form.IPropertySelectionModel;

import pl.model.DayModel;
import pl.model.LocalisedMonthsModel;
import pl.model.YearModel;

public abstract class DateInput extends BaseComponent implements

PageBeginRenderListener {

	@Parameter(required = true)
	public abstract Date getDate();
	public abstract void setDate(Date d);

	private Calendar c = Calendar.getInstance();

	public void pageBeginRender(PageEvent event) {

		c.setTime(getDate() == null ? new Date() : getDate());

	}

	public IPropertySelectionModel getDaysModel() {

		return new DayModel();

	}

	public IPropertySelectionModel getMonthsModel() {

		return new LocalisedMonthsModel(getPage().getLocale());

	}

	public IPropertySelectionModel getYearsModel() {

		return new YearModel();

	}

	public int getDay() {

		return c.get(Calendar.DATE);

	}

	public void setDay(int day) {

		c.set(Calendar.DATE, day);

	}

	public int getMonth() {

		return c.get(Calendar.MONTH);

	}

	public void setMonth(int month) {

		c.set(Calendar.MONTH, month);

	}

	public int getYear() {

		return c.get(Calendar.YEAR);

	}

	public void setYear(int year) {

		c.set(Calendar.YEAR, year);

		setDate(c.getTime());

	}

}
